using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using Microsoft.Practices.CompositeWeb;
using Microsoft.Practices.ObjectBuilder;
using AppExtensions.Controllers;
using System.Web;
using AppExtensions.Views;
using AppExtensions.Presenters;

namespace AspNetExtensions.Pages
{
    public interface IContextView : IBaseView
    {
        IDictionary<string, object> ContextObject { get; }
    }

    public interface IContextPresenter
    {
        IContextView View { get; set; }
    }

    public class ContextPresenter : BasePresenter<IContextView>
    {
        public ContextPresenter(IContextView view, NameValueCollection parameters)
            : base(view)
        {
        }
	
        public object this[string key]
        {
            get
            {
                return this.View.ContextObject[key];
            }
            set
            {
                this.View.ContextObject[key] = value;
            }
        }
    }

    public class ContextPage : ContextPage<ContextPresenter>
    {

    }

    public class ContextPage<TPresenter> : BasePage<TPresenter , IContextView>, IContextView where TPresenter : BasePresenter<IContextView>
    {
        private TPresenter _presenter;

        public ContextPage()
            : base()
        {
            this.contextObject = new Dictionary<string, object>();
            this.Load += ContextPage_Load;
        }

        private IDictionary<string, object> contextObject;

        public IDictionary<string, object> ContextObject
        {
            get { return contextObject; }
        }

        [CreateNew]
        public TPresenter Presenter
        {
            get { return _presenter; }
            protected set
            {
                _presenter = value;
                _presenter.View = this;
            }
        }

        protected void ContextPage_Load(object sender, EventArgs e)
        {            
            if (!this.IsPostBack)
            {
                this.Presenter.OnViewInitialized();
                //this.Presenter.Show(lifeflowId);
            }
            this.Presenter.OnViewLoaded();

        }
    }
}
